<?php

namespace App\Http\Controllers\home;

use App\Http\Model\Fav;
use Illuminate\Http\Request;
use App\Http\Model\Goods;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Cookie;
use App\Http\Controllers\home\BaseController;
use App\Http\Model\Cart;
use App\Http\Model\Order;
class ItemController extends BaseController
{
    /**
     * 展示商品信息
     * @param $id
     */
    public function index($id){
        //调用浏览历史记录方法
        $this->His($id);
    	$item = Goods::where('goods_id',$id)->get()->first();
          return view('home.goods.item',[
              'item'=>$item
          ]);
    }

    /**
     * 生成商品浏览记录
     * @param $id
     * @return bool
     */
    public function His($id)
    {
        if (!isset($_SESSION['his'])) {
            $his[] = $id;
            $_SESSION['his'] = $his;
        } else {
            $his = $_SESSION['his'];
            if (in_array($id, $his)) {
                return false;
            }
            $his[] = $id;
            if (count($his) > 5) {
                array_shift($his);
            }
            $_SESSION['his'] = $his;
        }
    }

    /**
     * ajax提交post商品收藏
     */
    public function shoucang(Request $req){
        /** 判断当前用户是否登陆 */
        $uid = Cookie::get('id');
        $goods_id = $req -> gid;
        if(!$uid){
            /** 跳转登陆页面 */
            /** 记录当前请求action */
            session(['backUrl'=>"/shoucang?gid=$goods_id"]);
            return 0;
        }

        /** 获取用户信息 */
        /** 判断当前用户是否收藏过该商品 */
        if(!Fav::where('uid','=',$uid,'and','gid','=',$goods_id)->first()){
            /** 进行存储 */
            $fav = new Fav();
            $fav -> uid = $uid;
            $fav -> gid = $goods_id;
            if( $fav -> save()){
                return 2;
            }else{
                return 3;
            }
        }else{
            return 1;
        }
    }

    /**
     * 添加购物车
     * @param $goods_id
     * @param $num
     */
    public function cartadd($goods_id,$num)
    {  

      $id = Cookie::get('id');
      if(!$id)
      {
        echo '0';
      }
      else
      {   
        $cart = new Cart();
        $cart_info = $cart -> where('goods_id','=',$goods_id,'and','user_id','=',$id)->get()->first();
        if(!$cart_info)
        {
          $row =  Goods::where('goods_id',$goods_id)->get()->first();
          $cart -> user_id = $id;
          $cart -> goods_id = $goods_id;
          $cart -> goods_name = $row -> goods_name;
          $cart -> goods_num = $num;
          $cart -> goods_price = $row -> shop_price;
          $cart -> add_time = time();
          $cart -> goods_status = 1;
          $cart -> goods_img = $row -> goods_ori;
          $cart -> save();
          echo '1';
        }
        else
        {
          $cart_info->goods_num+=$num;
          $cart_info->save();
          echo '1';
        }

      }

    }

    public function cart()
    {  
      $id = Cookie::get('id');
      if(!$id)
      {
        return redirect('login');
      }
    	else
      {
        $cart_info = Cart::where('user_id',$id)->get();
        return view('home.goods.cart',['cart_info'=>$cart_info]);
      }
    }

    public function order(Request $req)
    { 
      $id = Cookie::get('id');
      $order = new Order;
      if(!$id)
      {
        return redirect('login');
      }
      $num = $req -> num;
      $goods_id = $req -> goods_id;
      $row = Goods::where('goods_id',$goods_id)->get()->first();
      echo $this->ordersn();
      return view('home.goods.order');
    }

    /**
     * 移除购物车商品
     */
    public function cart_del(Request $req){
        $id = $req -> cid;
        /** 删除数据 */
        if(Cart::where('cart_id',$id)->delete()){
            return 1;
        }else{
            return 0;
        }
    }


    public function ordersn()
    {
      $str = date('YmdHis');
      $str.=mt_rand(1000,9999);
      return $str;
    }













}
